package com.hepinghospital.user.ui.kotlin

import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.gyf.immersionbar.ImmersionBar
import com.hepinghospital.user.R
import com.hepinghospital.user.base.BaseActivity
import com.hepinghospital.user.ui.activity.userinfo.LoginActivity
import jz.joyoung.robot.util.PreferencesHelper
import kotlinx.android.synthetic.main.activity_guide.*

/**
 * @Description: 引导页
 * @Author: lzp
 * @CreateDate: 2023/2/23 15:48
 * @UpdateRemark: 更新说明
 */
class GuideActivity : BaseActivity() {

    val views = ArrayList<View>()

    override fun initData() {
        views.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_one, null))
        views.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_two, null))
        views.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_three, null))
    }

    override fun getLayoutResource(): Int = R.layout.activity_guide

    override fun initViews() {
        viewPager.adapter = GuideAdapter()
        viewPager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == views.size - 1) {
                    btn_login.visibility = View.VISIBLE
                } else {
                    btn_login.visibility = View.GONE
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        iv_skip.setOnClickListener { toLoginActivity() }
        btn_login.setOnClickListener { toLoginActivity() }
    }

    override fun doBeforeSetcontentView() {
        ImmersionBar.with(this).keyboardEnable(true).transparentStatusBar().fitsSystemWindows(false)
            .navigationBarColor(R.color.black).autoDarkModeEnable(true).init()

    }

    private fun toLoginActivity() {
        PreferencesHelper.getInstance().set(mContext, "isFirstInstall", false)
        Handler().postDelayed({
            startActivity(LoginActivity::class.java)
            finish()
        }, 0)
    }

    inner class GuideAdapter : PagerAdapter() {
        // item的个数
        override fun getCount(): Int {
            return views.size
        }

        override fun isViewFromObject(arg0: View, arg1: Any): Boolean {
            return arg0 === arg1
        }

        // 初始化item布局
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view: View = views[position]
            container.addView(view)
            return view
        }

        // 销毁item
        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }
    }
}